<?php
// 应用公共文件

function response_error($code = -1, $msg = null)
{
    return response_success('', $code, $msg);
}

function response_success($data = '', $code = 0, $msg = null)
{
    if ($msg === null) {
        switch ($code) {
            case 0:
                $msg = 'success';
                break;
            case -1:
                $msg = 'Request Error';
                break;
            case -2:
                $msg = 'Not Authorize';
                break;
        }
    }
    $obj = response(['code' => $code, 'msg' => $msg, 'data' => $data], 200, [], 'json');
    //$obj->send();
    return $obj;
}

function is_wechat()
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_agent, 'MicroMessenger') === false) {
        return false;
    }
    return true;
}

function my_htmlspecialchars($str)
{
    return htmlspecialchars($str, ENT_QUOTES);
}

/**
 * 生成随机字符串
 * @param int $length 生成长度
 * @return string
 */
function randStr($length = 10,$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

/**
 * 去掉字符串中表情符号
 * @param string $str 字符串
 * @return mixed
 */
function filterEmoji($str)
{
    $str = preg_replace_callback('/./u', function (array $match) {
        return strlen($match[0]) >= 4 ? '' : $match[0];
    }, $str);
    return $str;
}


